package com.Ahmad.FingerLock;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class MyLockServer extends Service {

	MyLockBroadcastReceiver mMasterResetReciever;

	Intent startIntent = null;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		mMasterResetReciever = new MyLockBroadcastReceiver();
		registerReceiver(mMasterResetReciever, filter);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		onStart(intent, startId);
		super.onStartCommand(intent, flags, startId);
		if (intent != null && (intent.getBooleanExtra("screen_state", false))) {
			Intent i = new Intent();
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.setClass(this, MainActivity.class);
			startActivity(i);
		}
		return 1;

	}

	@Override
	public void onStart(Intent intent, int startId) {
		
	}

	@Override
	public void onDestroy() {
		unregisterReceiver(mMasterResetReciever);
		if (startIntent != null) {
			startService(startIntent);
		}
	}

	public class MyLockBroadcastReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
				Log.i("check", "broadcast on receive screen lock off ");
				boolean a = true;
				Intent localIntent = new Intent(context, MyLockServer.class);
				localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				localIntent.putExtra("screen_state", a);
				context.startService(localIntent);
			} else if (!intent.getAction().equals(
					"android.intent.action.SCREEN_ON")) {
				Log.i("check", "broadcast on receive screen lock on");
			}
		}
	}

}
